package ochart.serialization.json.oChart.charts
{
	import ochart.core.ChartBase;
	import ochart.core.ChartObjectFactory;
	import ochart.core.XYChart;
	import ochart.elements.background.Background;
	import ochart.elements.labels.DataTip;
	import ochart.elements.labels.Title;
	import ochart.serialization.json.JSON;
	import ochart.serialization.json.oChart.JSONSerializer;
	
	public class ChartSerializer extends JSONSerializer
	{
		public function ChartSerializer()
		{
			super();
		}
		
		override public function fromJSON(json:Object):Boolean
		{
			if(!super.fromJSON(json))
			{
				return false;
			}
			
			var chart:ChartBase = this.target as ChartBase;
			if(json[Title.ALIAS])
			{
				var title:Title = ChartObjectFactory.instance.create(Title.ALIAS) as Title;
				title.chart = chart;
				title.data = json[Title.ALIAS];
				chart.title = title;
			}
			if(json[Background.ALIAS])
			{
				var background:Background = ChartObjectFactory.instance.create(Background.ALIAS) as Background;
				background.chart = chart;
				background.data = json[Background.ALIAS];
				if(!json[Background.ALIAS]["height"])
				{
					background.height = chart.height;
				}
				if(!json[Background.ALIAS]["width"])
				{
					background.width = chart.width;
				}
				chart.background = background;
			}
			if(json[DataTip.ALIAS])
			{
				var dataTip:DataTip = ChartObjectFactory.instance.create(DataTip.ALIAS) as DataTip;
				dataTip.chart = chart;
				dataTip.data = json[DataTip.ALIAS];
				chart.dataTip = dataTip;
			}
			
			return true;
		}
		
		override public function toJSON():JSON
		{
			return null;
		}
	}
}